# Makefile for AI Glass Linux Test Framework

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -pthread -D_GNU_SOURCE
LDFLAGS = -pthread

# 目录设置
SRC_DIR = src
INC_DIR = include
BUILD_DIR = build
TEST_DIR = tests

# 目标文件
DAEMON_TARGET = $(BUILD_DIR)/ai_glass_test
CLIENT_TARGET = $(BUILD_DIR)/ai_glass_cmd
TEST_TARGET = $(BUILD_DIR)/unit_test

# 源文件 (分离daemon和client源文件)
DAEMON_SOURCES = $(SRC_DIR)/daemon_main.c $(SRC_DIR)/daemon_service.c $(SRC_DIR)/test_framework_core.c $(SRC_DIR)/thread_management.c $(SRC_DIR)/command_handlers.c
CLIENT_SOURCES = $(SRC_DIR)/client_cmd.c
TEST_SOURCES = $(TEST_DIR)/unit_test.c $(SRC_DIR)/test_framework_core.c $(SRC_DIR)/thread_management.c $(SRC_DIR)/command_handlers.c

# 对象文件
DAEMON_OBJECTS = $(DAEMON_SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
CLIENT_OBJECTS = $(CLIENT_SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
TEST_OBJECTS = $(TEST_SOURCES:%.c=$(BUILD_DIR)/%.o)

# 头文件依赖
INCLUDES = -I$(INC_DIR)

# 默认目标
all: $(DAEMON_TARGET) $(CLIENT_TARGET)

# 创建构建目录
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# 编译后台服务程序
$(DAEMON_TARGET): $(DAEMON_OBJECTS) | $(BUILD_DIR)
	$(CC) $(DAEMON_OBJECTS) -o $@ $(LDFLAGS)
	@echo "后台服务程序编译完成: $@"

# 编译客户端程序
$(CLIENT_TARGET): $(CLIENT_OBJECTS) | $(BUILD_DIR)
	$(CC) $(CLIENT_OBJECTS) -o $@ $(LDFLAGS)
	@echo "客户端程序编译完成: $@"

# 编译测试程序
$(TEST_TARGET): $(TEST_OBJECTS) | $(BUILD_DIR)
	$(CC) $(TEST_OBJECTS) -o $@ $(LDFLAGS)
	@echo "测试程序编译完成: $@"

# 编译对象文件 - daemon源文件
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译对象文件 - 测试源文件
$(BUILD_DIR)/%.o: $(TEST_DIR)/%.c | $(BUILD_DIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 调试版本
debug: CFLAGS += -g -DDEBUG
debug: clean all
	@echo "调试版本编译完成"

# 发布版本
release: CFLAGS += -O2 -DNDEBUG
release: clean all
	@echo "发布版本编译完成"

# 编译测试
test: $(TEST_TARGET)
	@echo "测试程序编译完成"

# 清理
clean:
	rm -rf $(BUILD_DIR)
	@echo "清理完成"

# 安装
install: $(DAEMON_TARGET) $(CLIENT_TARGET)
	cp $(DAEMON_TARGET) /usr/local/bin/ai_glass_test
	cp $(CLIENT_TARGET) /usr/local/bin/ai_glass_cmd
	@echo "安装完成到 /usr/local/bin/"

# 卸载
uninstall:
	rm -f /usr/local/bin/ai_glass_test
	rm -f /usr/local/bin/ai_glass_cmd
	@echo "卸载完成"

# 运行测试
run_test: $(DAEMON_TARGET) $(CLIENT_TARGET)
	@echo "运行测试程序..."
	./$(DAEMON_TARGET) --help
	./$(CLIENT_TARGET) --help

# 代码格式化
format:
	find $(SRC_DIR) $(INC_DIR) -name "*.c" -o -name "*.h" | xargs clang-format -i
	@echo "代码格式化完成"

# 静态分析
analyze:
	cppcheck --enable=all --std=c99 $(SRC_DIR)
	@echo "静态分析完成"

# 内存检查
memcheck: debug
	valgrind --leak-check=full --show-leak-kinds=all ./$(DAEMON_TARGET) --help

start_daemon: $(DAEMON_TARGET)
	@echo "启动后台服务..."
	./$(DAEMON_TARGET) --daemon

stop_daemon: $(CLIENT_TARGET)
	@echo "停止后台服务..."
	./$(CLIENT_TARGET) AT+EXIT || true

service_status: $(CLIENT_TARGET)
	@echo "检查服务状态..."
	./$(CLIENT_TARGET) --status

# 生成文档
docs:
	doxygen Doxyfile
	@echo "文档生成完成"

# 打包
package: release
	tar -czf ai_glass_test_framework.tar.gz $(SRC_DIR) $(INC_DIR) $(TEST_DIR) Makefile README.md
	@echo "打包完成: ai_glass_test_framework.tar.gz"

# 显示帮助
help:
	@echo "可用目标:"
	@echo "  all           - 编译所有程序 (默认)"
	@echo "  debug         - 编译调试版本"
	@echo "  release       - 编译发布版本"
	@echo "  test          - 编译测试程序"
	@echo "  clean         - 清理编译文件"
	@echo "  install       - 安装到系统"
	@echo "  uninstall     - 从系统卸载"
	@echo "  run_test      - 运行测试"
	@echo "  start_daemon  - 启动后台服务"
	@echo "  stop_daemon   - 停止后台服务"
	@echo "  service_status- 检查服务状态"
	@echo "  format        - 格式化代码"
	@echo "  analyze       - 静态代码分析"
	@echo "  memcheck      - 内存泄漏检查"
	@echo "  docs          - 生成文档"
	@echo "  package       - 打包源码"
	@echo "  help          - 显示此帮助"

# 声明伪目标
.PHONY: all debug release clean install uninstall test format analyze memcheck docs package help

# 依赖关系
$(BUILD_DIR)/main.o: $(INC_DIR)/test_framework.h
$(BUILD_DIR)/test_framework_core.o: $(INC_DIR)/test_framework.h
$(BUILD_DIR)/thread_management.o: $(INC_DIR)/test_framework.h
$(BUILD_DIR)/command_handlers.o: $(INC_DIR)/test_framework.h